﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Bangle.DBUtility;

namespace Bangle.DAL
{
    //T_Supplier
    public partial class T_SupplierDAL
    {

        public bool Exists(Guid SupplierId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Supplier");
            strSql.Append(" where ");
            strSql.Append(" SupplierId = @SupplierId  ");
            SqlParameter[] parameters = {
					new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = SupplierId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Bangle.Model.T_Supplier model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Supplier(");
            strSql.Append("SupplierId,SupplierCode,SupplierName,Description,CreateUserId,CreateTime,UpdateUserId,UpdateTime,Valid,Contact,Phone,QQ,Tel,Adress,Webside,SupplierTypeId,Gender,Wangwang");
            strSql.Append(") values (");
            strSql.Append("@SupplierId,@SupplierCode,@SupplierName,@Description,@CreateUserId,@CreateTime,@UpdateUserId,@UpdateTime,@Valid,@Contact,@Phone,@QQ,@Tel,@Adress,@Webside,@SupplierTypeId,@Gender,@Wangwang");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SupplierCode", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@SupplierName", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Description", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@Contact", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@Phone", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@QQ", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Tel", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Adress", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@Webside", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@SupplierTypeId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Gender", SqlDbType.Int,4) ,            
                        new SqlParameter("@Wangwang", SqlDbType.VarChar,100)             
              
            };

            parameters[0].Value = model.SupplierId;
            parameters[1].Value = model.SupplierCode;
            parameters[2].Value = model.SupplierName;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.CreateUserId;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.UpdateUserId;
            parameters[7].Value = model.UpdateTime;
            parameters[8].Value = model.Valid;
            parameters[9].Value = model.Contact;
            parameters[10].Value = model.Phone;
            parameters[11].Value = model.QQ;
            parameters[12].Value = model.Tel;
            parameters[13].Value = model.Adress;
            parameters[14].Value = model.Webside;
            parameters[15].Value = model.SupplierTypeId;
            parameters[16].Value = model.Gender;
            parameters[17].Value = model.Wangwang;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Bangle.Model.T_Supplier model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Supplier set ");

            strSql.Append(" SupplierId = @SupplierId , ");
            strSql.Append(" SupplierCode = @SupplierCode , ");
            strSql.Append(" SupplierName = @SupplierName , ");
            strSql.Append(" Description = @Description , ");
            strSql.Append(" CreateUserId = @CreateUserId , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" UpdateUserId = @UpdateUserId , ");
            strSql.Append(" UpdateTime = @UpdateTime , ");
            strSql.Append(" Valid = @Valid , ");
            strSql.Append(" Contact = @Contact , ");
            strSql.Append(" Phone = @Phone , ");
            strSql.Append(" QQ = @QQ , ");
            strSql.Append(" Tel = @Tel , ");
            strSql.Append(" Adress = @Adress , ");
            strSql.Append(" Webside = @Webside , ");
            strSql.Append(" SupplierTypeId = @SupplierTypeId , ");
            strSql.Append(" Gender = @Gender , ");
            strSql.Append(" Wangwang = @Wangwang  ");
            strSql.Append(" where SupplierId=@SupplierId  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SupplierCode", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@SupplierName", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Description", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@Contact", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@Phone", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@QQ", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Tel", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Adress", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@Webside", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@SupplierTypeId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Gender", SqlDbType.Int,4) ,            
                        new SqlParameter("@Wangwang", SqlDbType.VarChar,100)             
              
            };

            parameters[0].Value = model.SupplierId;
            parameters[1].Value = model.SupplierCode;
            parameters[2].Value = model.SupplierName;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.CreateUserId;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.UpdateUserId;
            parameters[7].Value = model.UpdateTime;
            parameters[8].Value = model.Valid;
            parameters[9].Value = model.Contact;
            parameters[10].Value = model.Phone;
            parameters[11].Value = model.QQ;
            parameters[12].Value = model.Tel;
            parameters[13].Value = model.Adress;
            parameters[14].Value = model.Webside;
            parameters[15].Value = model.SupplierTypeId;
            parameters[16].Value = model.Gender;
            parameters[17].Value = model.Wangwang;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid SupplierId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Supplier ");
            strSql.Append(" where SupplierId=@SupplierId ");
            SqlParameter[] parameters = {
					new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = SupplierId;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Bangle.Model.T_Supplier GetModel(Guid SupplierId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SupplierId, SupplierCode, SupplierName, Description, CreateUserId, CreateTime, UpdateUserId, UpdateTime, Valid, Contact, Phone, QQ, Tel, Adress, Webside, SupplierTypeId, Gender, Wangwang  ");
            strSql.Append("  from T_Supplier ");
            strSql.Append(" where SupplierId=@SupplierId ");
            SqlParameter[] parameters = {
					new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = SupplierId;


            Bangle.Model.T_Supplier model = new Bangle.Model.T_Supplier();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["SupplierId"].ToString() != "")
                {
                    model.SupplierId = Guid.Parse(ds.Tables[0].Rows[0]["SupplierId"].ToString());
                }
                model.SupplierCode = ds.Tables[0].Rows[0]["SupplierCode"].ToString();
                model.SupplierName = ds.Tables[0].Rows[0]["SupplierName"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                if (ds.Tables[0].Rows[0]["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
                }
                model.CreateTime = ds.Tables[0].Rows[0]["CreateTime"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateUserId"].ToString() != "")
                {
                    model.UpdateUserId = int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
                }
                model.UpdateTime = ds.Tables[0].Rows[0]["UpdateTime"].ToString();
                if (ds.Tables[0].Rows[0]["Valid"].ToString() != "")
                {
                    model.Valid = int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
                }
                model.Contact = ds.Tables[0].Rows[0]["Contact"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                model.Adress = ds.Tables[0].Rows[0]["Adress"].ToString();
                model.Webside = ds.Tables[0].Rows[0]["Webside"].ToString();
                if (ds.Tables[0].Rows[0]["SupplierTypeId"].ToString() != "")
                {
                    model.SupplierTypeId = int.Parse(ds.Tables[0].Rows[0]["SupplierTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Gender"].ToString() != "")
                {
                    model.Gender = int.Parse(ds.Tables[0].Rows[0]["Gender"].ToString());
                }
                model.Wangwang = ds.Tables[0].Rows[0]["Wangwang"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Supplier ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM T_Supplier ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Supplier ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.MenuId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Supplier T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

